import pandas as pd
import numpy as np
import os
import matplotlib.pyplot as plt
import warnings
import math
from pyecharts import options as opts
from pyecharts.charts import Bar

warnings.filterwarnings("ignore")  # 取消警告内容
from pyecharts.globals import ThemeType

plt.rcParams["font.sans-serif"] = ["SimHei"]  # 用来正常显示中文标签
plt.rcParams["axes.unicode_minus"] = False  # 用来正常显示负号
os.chdir('D:/Desktop_folder/Python_w/jielin/')
from pyecharts.charts import Line


def word_cut(word, method):
    pos = word.find("-")
    pos1 = word.find("万/月")
    pos2 = word.find("万/年")
    pos3 = word.find("千/月")
    if pos1 != -1:
        low = float(word[:pos]) * 10
        hig = float(word[pos + 1:pos1]) * 10
    elif pos2 != -1:
        low = float(word[:pos]) * 10 / 12
        hig = float(word[pos + 1:pos2]) * 10 / 12
    elif pos3 != -1:
        low = float(word[:pos])
        hig = float(word[pos + 1:pos3])
    else:
        low = np.nan
        hig = np.nan
    if method == "low":
        return low
    else:
        return hig


def change_edu(word, method):
    try:
        edu = word.split(" | ")[2]
        if "招" in edu:
            edu = "无要求"
    except:
        edu = "无要求"
    if method == "none":
        return edu


def change_exp(word, method):
    try:
        exp = word.split(" | ")[1]
        if "经验" not in exp:
            exp = "无需经验"
    except:
        exp = "无需经验"
    if method == "none":
        return exp


# 第一步，清洗数据
data = pd.read_csv("大数据.csv", engine="python", error_bad_lines=False)
data1 = data.dropna(subset=["职位名称"])
data1 = data1.dropna(subset=["薪水"])
data1 = data1.dropna(subset=["职位福利"])
data1 = data1.dropna(subset=["简介"])
"""
    对薪水的单位进行转换
"""
data1["low_salary"] = data1["薪水"].apply(word_cut, method="low")
data1["hig_salary"] = data1["薪水"].apply(word_cut, method="hig")
data2 = data1.dropna(subset=["low_salary"])

"""
    对薪水进行平均化计算
"""
data2["ava_salary"] = data2.apply(lambda x: (x.hig_salary + x.low_salary) / 2, axis=1)  # 计算出平均薪水
data2["ln_salary"] = data2.apply(lambda x: math.log(x.ava_salary), axis=1)  # 计算出对数化的薪水

# 第二步，对工作地点进行规范,还有工作经验也需要进行规范
data2["city"] = data2.apply(lambda x: x.简介.split(" | ")[0].split("-")[0].strip(), axis=1)
data2["工作经验"] = data2["简介"].apply(change_exp, method="none")
data2["学历"] = data2["简介"].apply(change_edu, method="none")
data2["公司性质"] = data2.apply(lambda x: x.公司描述.split("|")[0], axis=1)
data2["公司规模"] = data2.apply(lambda x: x.公司描述.split("|")[1], axis=1)

# 第四步：删除无关列，并重命名便于观察
data3 = data2.drop(["职位详情链接", "公司名字", "职位id", "low_salary", "hig_salary"], axis=1)
data4 = data3.rename(
    columns={"工作经验": "exp", "学历": "edu", "职位福利": "welfare", "职位信息": "job_info", "公司性质": "companytype", "公司规模": "scale",
             "所属类别": "industry"})

# 第五步：删除学位列无值的数据
data4 = data4.dropna(subset=["edu"])
df_plot = data4

# 第六步：开始数据可视化
"""
    用柱状图观察各城市职位数量分布情况
"""
data_city = df_plot.city.value_counts()
# ax1=data_city.plot(kind="bar",figsize=(12,6),fontsize="14",rot=0,legend=True,grid=True)
# plt.title("各城市职位数量分布情况",color="blue",fontsize=24,y=1.05)
# for i in range(0,len(data_city)):
#    ax1.text(i-0.15,data_city[i]+30,data_city[i],fontsize=12,color="k")
c = (
    Bar(init_opts=opts.InitOpts(theme=ThemeType.PURPLE_PASSION))
        .add_xaxis(
        data_city.index.to_list()
    )
        .add_yaxis("城市", data_city.to_list())
        .set_global_opts(
        title_opts=opts.TitleOpts(title="各城市职位数量分布情况", subtitle="查看哪个职位数量排名靠前"),

    )
        .render("各城市职位数量分布情况.html")
)

"""
    用条形图观察不同学历层次岗位需求数量情况
"""
data_edu = df_plot.edu.value_counts()
# ax2=data_edu.plot(kind="barh",figsize=(12,6),fontsize="14",xlim=[0,4000],legend=True)
# ax2.set_legend=(["博士","本科"])
# plt.title("不同学历层次岗位需求数量情况",color="blue",fontsize=24,y=1.05)
# for i in range(0,len(data_edu)):
#    ax2.text(data_edu[i]+30,i-0.15,data_edu[i],fontsize=12,color="k")
c2 = (
    Bar(init_opts=opts.InitOpts(theme=ThemeType.PURPLE_PASSION))
        .add_xaxis(data_edu.index.to_list())
        .add_yaxis("学历", data_edu.to_list())
        .set_global_opts(
        title_opts=opts.TitleOpts(title="用条形图观察不同学历层次岗位需求数量情况"),
        datazoom_opts=opts.DataZoomOpts(orient="vertical"),
    )
        .render("用条形图观察不同学历层次岗位需求数量情况.html")
)

"""
工作经验漏斗图
"""
from pyecharts.charts import Funnel

print(df_plot.exp.value_counts().index.to_list())
print(df_plot.exp)
c3 = (
    Funnel(init_opts=opts.InitOpts(theme=ThemeType.WALDEN))
        .add(
        "工作经验",
        [list(z) for z in zip(df_plot.exp.value_counts().index.to_list(), df_plot.exp.value_counts().to_list())],
        label_opts=opts.LabelOpts(position="inside"),
    )
        .set_global_opts(title_opts=opts.TitleOpts(title="工作经验要求"))
        .render("工作经验漏斗图.html")
)

"""
    用箱图观察不同城市薪酬分布情况
"""
# ax3=df_plot.boxplot(column="ln_salary",by="city",figsize=(12,6),fontsize=14,widths=df_plot.city.value_counts()/2500)
# ax3.set_title("不同城市的薪酬分布情况",fontsize=24,color="blue",y=1.1)
# ax3.set_xlabel("城市",fontsize=14)
# ax3.set_ylabel("平均薪酬",fontsize=14)


"""
用箱图观察不同学历薪酬分布情况
"""
# ax4=df_plot.boxplot(column="ln_salary",by="edu",figsize=(12,6),fontsize=14,widths=0.5)
# ax4.set_title("不同学历层次的薪酬分布情况",fontsize=24,color="blue",y=1.1)
# ax4.set_xlabel("学历",fontsize=14)
# ax4.set_ylabel("平均薪酬",fontsize=14)


"""
用直方图观察硕士、本科、大专3个不同学历层次的薪酬分布情况
"""
# fig,ax5=plt.subplots(figsize=(12,8))
# ax5.set_title("硕士、本科、大专3个不同学历层次的薪酬分布情况",fontsize=24,color="blue",y=1.02)
# ax5.hist(x=df_plot[df_plot.edu=="硕士"].ln_salary,bins=20,facecolor="red",alpha=1)
# ax5.hist(x=df_plot[df_plot.edu=="本科"].ln_salary,bins=20,facecolor="green",alpha=0.5)
# ax5.hist(x=df_plot[df_plot.edu=="大专"].ln_salary,bins=20,facecolor="blue",alpha=0.5)
# ax5.legend(["硕士","本科","大专"])


"""
观察不同公司类型、不同公司规模的薪酬情况
"""
df_company = df_plot.groupby(["companytype", "scale"], as_index=False).mean()
x_list = []
y_list = []
for i in range(0, len(df_company.ava_salary)):
    x_list.append([(df_company["companytype"][i]), df_company["scale"][i]])
    y_list.append("%.2f" % df_company.ava_salary[i])

c6 = (
    Bar(init_opts=opts.InitOpts(
        width='1800px',
        height='700px',
        page_title='page',
        theme=ThemeType.MACARONS,
    ))
        .add_xaxis(x_list)
        .add_yaxis("薪酬情况", y_list)
        .reversal_axis()
        .set_series_opts(label_opts=opts.LabelOpts(position="right"))
        .set_global_opts(title_opts=opts.TitleOpts(title="观察不同公司类型、不同公司规模的薪酬情况，单位（千/月）"))
        .render("观察不同公司类型、不同公司规模的薪酬情况.html")
)
# for i in range(0,len(df_company.ava_salary)):
#     print(df_company)
# df_company=df_plot.groupby(["companytype","scale"]).mean()
# df_company.head()
# ax6=df_company.ava_salary.plot.barh(figsize=(28,20))
# ax6.set_title("不同公司类型、不同公司规模的平均薪酬情况",fontsize=24,color="blue",y=1.02)
# for i in range(0,len(df_company.ava_salary)):
#     ax6.text(df_company.ava_salary[i]+0.12,i-0.2,"%.2f"%df_company.ava_salary[i]+"千/月",fontsize=12,color="k")#添加数据标签

"""
观察不同城市、不同学历层次的薪酬情况
"""
df_city_edu = df_plot.groupby(["city", "edu"], as_index=False).mean()
x_list = []
y_list = []
for i in range(0, len(df_city_edu)):
    x_list.append([(df_city_edu["city"][i]), df_city_edu["edu"][i]])
    y_list.append("%.2f" % df_city_edu.ava_salary[i])

c6 = (
    Bar(init_opts=opts.InitOpts(
        width='1400px',
        height='600px',
        page_title='page',
        theme=ThemeType.MACARONS,
    ))
        .add_xaxis(x_list)
        .add_yaxis("薪酬情况", y_list)
        .reversal_axis()
        .set_series_opts(label_opts=opts.LabelOpts(position="right"))
        .set_global_opts(title_opts=opts.TitleOpts(title="观察不同城市、不同学历层次的薪酬情况，单位（千/月）"))
        .render("观察不同城市、不同学历层次的薪酬情况.html")
)
# df_city_edu=df_plot.groupby(["city","edu"]).mean()
# df_city_edu.head(10)
# ax7=df_city_edu.ava_salary.plot.barh(figsize=(28,30))
# ax7.set_title("不同公司城市、不同学历层次的平均薪酬情况",fontsize=24,color="blue",y=1.02)
# for i in range(len(df_city_edu)):
#     ax7.text(df_city_edu.ava_salary[i]+0.15,i-0.18,"%.2f"%df_city_edu.ava_salary[i]+"千/月",fontsize=12,color="k")#添加数据标签

"""
福利待遇的词云图
"""
from pyecharts.charts import WordCloud

# 创建停用词list
def stopwordslist(filepath):
    stopwords = [line.strip() for line in open(filepath, 'r', encoding='utf-8').readlines()]
    return stopwords


def movestopwords(sentence):
    stopwords = stopwordslist('cn_stopwords.txt')  # 这里加载停用词的路径
    santi_words = [x for x in sentence if len(x) > 1 and x not in stopwords]
    return santi_words


import jieba

seted = []  # 已经测试过的
res = []  # 最后结果
zhong = []
for i in df_plot.welfare:
    words = jieba.cut(i)
    santi_words = movestopwords(words)
    zhong += santi_words
for word in zhong:
    if word not in seted:
        seted.append(word)
        count = zhong.count(word)
        res.append((word, count))
    else:
        pass
c7 = (
    WordCloud()
        .add(series_name="福利待遇的词云图", data_pair=res, word_size_range=[6, 66])
        .set_global_opts(
        title_opts=opts.TitleOpts(
            title="福利待遇的词云图", title_textstyle_opts=opts.TextStyleOpts(font_size=23)
        ),
        tooltip_opts=opts.TooltipOpts(is_show=True),
    )
        .render("福利待遇的词云图.html")
)

"""
行业分布词云图
"""
seted = []  # 已经测试过的
res = []  # 最后结果
zhong = []
for i in df_plot.industry:
    words = jieba.cut(i)
    santi_words = movestopwords(words)
    zhong += santi_words
for word in zhong:
    if word not in seted:
        seted.append(word)
        count = zhong.count(word)
        res.append((word, count))
    else:
        pass
c7 = (
    WordCloud()
        .add(series_name="行业分布词云图", data_pair=res, word_size_range=[6, 66])
        .set_global_opts(
        title_opts=opts.TitleOpts(
            title="行业分布词云图", title_textstyle_opts=opts.TextStyleOpts(font_size=23)
        ),
        tooltip_opts=opts.TooltipOpts(is_show=True),
    )
        .render("行业分布词云图.html")
)
